package security_ability_api

import (
	"context"
	log "github.com/sirupsen/logrus"
	"google.golang.org/grpc"
	"qabx.net/safe-app-center/api/security_ability_api"
	env "qabx.net/safe-app-center/pkg/config/env"
)

var apiExternalClient security_ability_api.SecurityAbilityApiServiceClient

func getClient() security_ability_api.SecurityAbilityApiServiceClient {
	if apiExternalClient == nil {
		apiExternalClient = initClient()
	}
	return apiExternalClient
}

func initClient() security_ability_api.SecurityAbilityApiServiceClient {
	conn, err := grpc.Dial("dns:///"+env.EDGE_MANAGE_URL, grpc.WithInsecure())
	if err != nil {
		log.Errorf("api_internal client (litterwingNotifyClient) init failed :%s", err.Error())
		return nil
	}
	cacheClient := security_ability_api.NewSecurityAbilityApiServiceClient(conn)
	return cacheClient
}

func CallSecurityScan(pppoeName, scanId, scanType string) (*security_ability_api.ScanResponse, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	abilityClient := getClient()

	req := &security_ability_api.ScanRequest{
		PppoeName:          "",
		ScanId:             "",
		ScanType:           "",
		WaitScanDeviceInfo: nil,
	}
	return abilityClient.SecurityScan(ctx, req)
}

func CallSecurityScanPercent() (*security_ability_api.ScanPercentResponse, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	abilityClient := getClient()

	req := &security_ability_api.ScanRequest{
		PppoeName:          "",
		ScanId:             "",
		ScanType:           "",
		WaitScanDeviceInfo: nil,
	}
	return abilityClient.SecurityScanPercent(ctx, req)
}
